<?php

namespace app\index\controller;


use app\index\model\Column;
use app\index\model\AccessLog;
use app\index\model\ColumnPage;
use app\index\model\User;
use app\index\model\UserCookie;
use think\Controller;
use think\helper\hash\Md5;
use think\facade\Request;

/**
 * yangyou
 * 前端基类
 * Class Base
 * @package app\admin\controller
 */
class Base extends Controller
{
    //初始化
    protected function _initialize(){
        $user_info = session('user_info');
        $this->assign("user_info", $user_info);
        $UserCookie = new UserCookie();
        //如果没有登陆就用cookie
        if(empty(cookie('cookie_name'))){
            $cookie_value = $this->getRandomString(30);
            cookie('cookie_name',$cookie_value,946080000);//存三十年
            //第一次浏览就创建user_cookie
            $cookie_key = md5(cookie('cookie_name'));
            //获取上一个页面来源的URL
            $referer = '';
            if(isset($_SERVER['HTTP_REFERER'])) {
                $referer = $_SERVER['HTTP_REFERER'];
            }
            $UserCookie->mySave(["key"=>$cookie_key,"create_ip"=>request()->ip(),"create_time"=>time(),"create_source"=>1,"referer"=>$referer]);
        }
        //添加访问日志-（本地不添加访问日志）
        if(request()->ip() != "127.0.0.1") {
            if(!request()->isAjax()) {//不是ajax请求添加
                $information_id = input("information_id",0);//资讯ID
                $drawing_id = input("drawing_id/d",0);//图纸ID
                $AccessLog = new AccessLog();
                $AccessLog->setTable($AccessLog->setTableName());
                $AccessLog->add([
                    "information_id"    => $information_id,
                    "drawing_id"    => $drawing_id,
                ]);
//                $this->assign("access_id", $AccessLog->access_id);//获取添加ID
            }
        }
        if (!session('host_name')){
            session('host_name',request()->host());
        }

        //底部栏目
        $ColumnPage = new ColumnPage();
        $columnPageList = $ColumnPage->columnPageList($substation_id="1",$parent_id ="0");
        foreach ($columnPageList as $key=>$val){
            $columnPageList[$key]['childrenList'] = $ColumnPage->columnPageList($substation_id="1",$val['page_id']);

        }
        $this->assign("bottomPageList", $columnPageList);
        //头部栏目
        $column = new Column();
        $columnList = $column->columnList();
        $baseUrl = request()->baseUrl();
        $this->assign("columnList", $columnList);
        $this->assign("baseUrl", $baseUrl);
    }

    //随机字符串
    function getRandomString($len, $chars=null)
    {
        if (is_null($chars)){
            $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
        }
        mt_srand(10000000*(double)microtime());
        for ($i = 0, $str = '', $lc = strlen($chars)-1; $i < $len; $i++){
            $str .= $chars[mt_rand(0, $lc)];
        }
        return $str;
    }

    // HTTP请求（支持HTTP/HTTPS，支持GET/POST)//验证码短信的请求
    function http_request($url, $data = null)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (! empty($data)) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

    /**
     * Author:yang you
     * Time:2018-12-6 10:00:43
     * Describe:链接获取参数链接(头部栏目不限的按钮)
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function paraUrl($structure,$house){
        if(!empty($structure)){
            if(!empty($house)){
                $paraUrl['houseStyle'] = "?structure=".$structure;
                $paraUrl['structureStyle'] = "?house=".$house;
                $paraUrl['drawingfloor'] = $paraUrl['drawingStyle'] = substr(request()->url(),strlen(request()->baseUrl()));
            }else{
                $paraUrl['houseStyle'] = "?structure=".$structure;
                $paraUrl['structureStyle'] = "";
                $paraUrl['drawingfloor'] = $paraUrl['drawingStyle'] = substr(request()->url(),strlen(request()->baseUrl()));
            }
        }elseif($house){
            $paraUrl['houseStyle'] = "";
            $paraUrl['structureStyle'] = "?&house=".$house;
            $paraUrl['drawingfloor'] = $paraUrl['drawingStyle'] = substr(request()->url(),strlen(request()->baseUrl()));
        }else{
            $paraUrl['houseStyle'] = "";
            $paraUrl['structureStyle'] = "";
            $paraUrl['drawingfloor'] = $paraUrl['drawingStyle'] = substr(request()->url(),strlen(request()->baseUrl()));
        }
        return $paraUrl;
    }





}
